package com.fone.player.play.adapter;

import java.util.List;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.client.RcmdRst.Rcmd;

/**
 * 本地视频相关adapter
 * 
 * @author kongxiaojun
 * @since 2014-4-30
 */
public class LocalVideoRelatedAdapter extends BaseAdapter {

	private List<Rcmd> rcmds;

	private String shost;

	public LocalVideoRelatedAdapter(List<Rcmd> rcmds,String shost) {
		super();
		this.rcmds = rcmds;
		this.shost = shost;
	}

	@Override
	public int getCount() {
		if (rcmds == null) {
			return 0;
		} else {
			return rcmds.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (rcmds == null) {
			return null;
		} else {
			return rcmds.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		VideoHolder holder = null;
		Rcmd rcmd = rcmds.get(position);

		if (convertView == null) {
			convertView = LayoutInflater.from(ApplicationManage.getAplicationManageInstance()).inflate(R.layout.full_video_related_item, null);
			holder = new VideoHolder((ImageView) convertView.findViewById(R.id.full_related_video_icon), (TextView) convertView.findViewById(R.id.full_related_video_name));
		} else {
			holder = (VideoHolder) convertView.getTag();
		}
		if (!TextUtils.isEmpty(rcmd.pic)) {
			String picUrl = rcmd.pic.replace("[shost]", shost);
			ImageDownloadModule.getInstance().download(picUrl, holder.videoIcon);
		}
		holder.videoName.setText(rcmd.name);
		convertView.setTag(holder);
		return convertView;
	}

	private class VideoHolder {
		public ImageView videoIcon;
		public TextView videoName;

		public VideoHolder(ImageView videoIcon, TextView videoName) {
			super();
			this.videoIcon = videoIcon;
			this.videoName = videoName;
		}
	}

	public void setData(String shost, List<Rcmd> rcmds) {
		this.rcmds = rcmds;
		this.shost = shost;
		notifyDataSetChanged();
	}

}
